import { _decorator, Component, Node, Sprite, Label, RichText, Canvas, Layout, ScrollView, ScrollBar, ProgressBar, Toggle, ToggleContainer, PageView, PageViewIndicator, Slider, BlockInputEvents, Graphics, PolygonCollider2D, BoxCollider2D, VideoPlayer, Vec2, sp, UITransform, Vec3, Button } from 'cc';
import GImage from '../core/GImage';

export default class CCUtils{

    //node.addChild 当你已经有parent的情况下再addChild会出现看不到的问题
    public static addChild(child:Node,parent:Node)
    {
        let lastParent:Node = child.parent;

        if(lastParent!=null)
        {
            child.removeFromParent();
        }
        child.setParent(parent);
    }

    //node避免触发onEnable onDisable
    public static setActive(node:Node,active:boolean)
    {
        if(node.active == active)
        {
            return;
        }
        node.active = active;
    }

    public static findChild(node:Node,name:string):Node
    {
        if(name=="")
        {
            return node;
        }
        let splitArr = name.split("/");
        let tempNode:Node = node;
        for(let i = 0;i<splitArr.length;i++)
        {
            tempNode = tempNode.getChildByName(splitArr[i]);
            if(tempNode==null)
            {
                break;
            }
        }
        return tempNode;
    }

    public static getSprite(node:Node,name:string):Sprite
    {
        return this.findChild(node,name).getComponent(Sprite);
    }

    public static getButton(node:Node,name:string):Button
    {
        return this.findChild(node,name).getComponent(Button);
    }

    public static getGImage(node:Node,name:string):GImage
    {
        return this.findChild(node,name).getComponent(GImage);
    }

    public static getLabel(node:Node,name:string):Label
    {
        return this.findChild(node,name).getComponent(Label);
    }

    public static getRichText(node:Node,name:string):RichText
    {
        return this.findChild(node,name).getComponent(RichText);
    }

    public static getCanvas(node:Node,name:string):Canvas
    {
        return this.findChild(node,name).getComponent(Canvas);
    }

    public static getWidget(node:Node,name:string):Canvas
    {
        return this.findChild(node,name).getComponent(Canvas);
    }

    public static getLayout(node:Node,name:string):Layout
    {
        return this.findChild(node,name).getComponent(Layout);
    }

    public static getScrollView(node:Node,name:string):ScrollView
    {
        return this.findChild(node,name).getComponent(ScrollView);
    }

    public static getScrollBar(node:Node,name:string):ScrollBar
    {
        return this.findChild(node,name).getComponent(ScrollBar);
    }

    public static getProgressBar(node:Node,name:string):ProgressBar
    {
        return this.findChild(node,name).getComponent(ProgressBar);
    }

    public static getToggle(node:Node,name:string):Toggle{
        return this.findChild(node,name).getComponent(Toggle);
    }

    public static getToggleContainer(node:Node,name:string):ToggleContainer
    {
        return this.findChild(node,name).getComponent(ToggleContainer);
    }

    public static getPageView(node:Node,name:string):PageView
    {
        return this.findChild(node,name).getComponent(PageView);
    }

    public static getPageviewIndicator(node:Node,name:string):PageViewIndicator
    {
        return this.findChild(node,name).getComponent(PageViewIndicator);
    }

    public static getSlider(node:Node,name:string):Slider
    {
        return this.findChild(node,name).getComponent(Slider);
    }

    public static getBlockInputEvents(node:Node,name:string):BlockInputEvents
    {
        return this.findChild(node,name).getComponent(BlockInputEvents);
    }

    public static getGraphics(node:Node,name:string):Graphics
    {
        return this.findChild(node,name).getComponent(Graphics);
    }

    // public static getAnimation(node:Node,name:string):Animation
    // {
    //     return this.findChild(node,name).getComponent(Animation);
    // }

    // public static getSpine(node:Node,name:string):SpineAnimationAdapter
    // {
    //     return SpineAnimationAdapter.get(this.findChild(node,name));
    // }

    public static getPolygonCollider(node:Node,name:string):PolygonCollider2D
    {
        return this.findChild(node,name).getComponent(PolygonCollider2D);
    }

    public static getBoxCollider(node:Node,name:string):BoxCollider2D
    {
        return this.findChild(node,name).getComponent(BoxCollider2D);
    }

    public static getVideoPlayer(node:Node,name:string):VideoPlayer
    {
        return this.findChild(node,name).getComponent(VideoPlayer);
    }

    public static isInBox(worldPos:Vec3,boxCollider:BoxCollider2D)
    {
        let innerPos = boxCollider.node.getComponent(UITransform).convertToNodeSpaceAR(worldPos);
        let isInX:boolean = false;
        let isInY:boolean = false;
        if(innerPos.x >=  (-boxCollider.node.getComponent(UITransform).anchorX * boxCollider.size.width) && (innerPos.x <= boxCollider.size.width*(1-boxCollider.node.getComponent(UITransform).anchorX)))
        {
            isInX = true;
        }

        if(innerPos.y >=  (-boxCollider.node.getComponent(UITransform).anchorY * boxCollider.size.height) && (innerPos.y <= boxCollider.size.height*(1-boxCollider.node.getComponent(UITransform).anchorY)))
        {
            isInY = true;
        }
        return isInX&&isInY;
    }

    public static findLastNumber(name:string):number
    {
        var reg = /.*([0-9]+)$/;
        var result = name.match(reg);
        if(result!=null)
        {
            return Number(result[1]);
        }
        return -1;
    }

    public static changeToNodePos(node1:Node,node2:Node)
    {
        let worldPos = node1.parent.getComponent(UITransform).convertToWorldSpaceAR(node1.getPosition());
        let nodeContentPos = node2.parent.getComponent(UITransform).convertToNodeSpaceAR(worldPos);
        return nodeContentPos;
    }

    public static getOrAddComponent<T extends Component>(node:Node,type: new()=>T):T
    {
        let component = node.getComponent(type);
        if(component)
        {
            return component;
        }
        return node.addComponent(type);
    }


    // public static createRegion(texture) {
    //     let skeletonTexture = new sp.SkeletonTexture()
    //     skeletonTexture.setRealTexture(texture)
    //     let page = new sp.spine.TextureAtlasPage()
    //     page.name = texture.name
    //     page.uWrap = sp.spine.TextureWrap.ClampToEdge
    //     page.vWrap = sp.spine.TextureWrap.ClampToEdge
    //     page.texture = skeletonTexture
    //     page.texture.setWraps(page.uWrap, page.vWrap)
    //     page.width = texture.width
    //     page.height = texture.height

    //     let region = new sp.spine.TextureAtlasRegion()
    //     region.page = page
    //     region.width = texture.width
    //     region.height = texture.height
    //     region.originalWidth = texture.width
    //     region.originalHeight = texture.height

    //     region.rotate = false
    //     region.u = 0
    //     region.v = 0
    //     region.u2 = 1
    //     region.v2 = 1
    //     region.texture = skeletonTexture
    //     return region
    // }

    public static shuffle(arr:Array<any>)
    {
        for(let i = arr.length - 1;i>=1;i--)
        {
            let random = Math.floor(Math.random()*i);
            let temp = arr[i];
            arr[i] = arr[random];
            arr[random] = temp;
        }
    }




}
